var GAME={}; //global namespace

var TEXTURE_PATH="resources/textures/";

if ( !window.requestAnimationFrame ) {

	window.requestAnimationFrame = ( function() {

		return window.webkitRequestAnimationFrame ||
		window.mozRequestAnimationFrame ||
		window.oRequestAnimationFrame ||
		window.msRequestAnimationFrame ||
		function( /* function FrameRequestCallback */ callback, /* DOMElement Element */ element ) {
			window.setTimeout( callback, 1000 / 60 );
		};

	} )();

}

var stats = new Stats();

//Align top-left
stats.domElement.style.position = 'absolute';
stats.domElement.style.left = '0px';
stats.domElement.style.top = '0px';

document.body.appendChild( stats.domElement );
setInterval( function () { stats.update(); }, 1000 / 60 );

init();
animate();

function init() {

	GAME.container = document.getElementById( 'container' );
	GAME.mouse = {x:0,y:0};

	GAME.camera = new THREE.Camera( 60, window.innerWidth / window.innerHeight, 1, 10000 );
	GAME.camera.distance=400;

	GAME.scene = new THREE.Scene();
	GAME.mesh={};
	
	//Pour pouvoir distinguer un peu la face obscure de la terre
	var blackBackground = new THREE.AmbientLight( 0x303030 );
	GAME.scene.addLight(blackBackground);
	
	//Earth
	var earthMaterial = new THREE.MeshLambertMaterial( { map: THREE.ImageUtils.loadTexture( TEXTURE_PATH+'earth.jpg' ) } );
	GAME.mesh.earth = new THREE.Mesh( new THREE.Sphere( 200, 100, 100 ), earthMaterial );
	GAME.scene.addObject(GAME.mesh.earth);

	//Sun
	var sunMaterial = new THREE.MeshBasicMaterial( { color:0xFFFFAA } );
	GAME.mesh.sun = new THREE.Mesh( new THREE.Sphere( 400, 50, 50 ), sunMaterial );
	GAME.mesh.sun.position.x = 2000;  
	GAME.scene.addObject(GAME.mesh.sun);
	//Sun pointlight
	var pointLight = new THREE.PointLight( 0xFFFFFF );
	pointLight.position = {x:2000, y:0, z:0};
	GAME.scene.addLight(pointLight);
	
	//Stars
	// create the particle variables
	var particleCount = 1000,
	    particles = new THREE.Geometry(),
	    pMaterial = new THREE.ParticleBasicMaterial({
	        color: 0xFFFFFF,
	        size: 10
	    });

	// now create the individual particles
	for(var p = 0; p < particleCount; p++) {

	    // create a particle with random
	    // position values, > 2500
	    var phi=Math.random()*Math.PI, theta=Math.random()*Math.PI*2, radius=Math.random()*5000+3000;
	    
		var pX = radius*Math.cos(theta)*Math.sin(phi),
	        pY = radius*Math.sin(theta)*Math.sin(phi),
	        pZ = radius*Math.cos(phi),
	        particle = new THREE.Vertex( new THREE.Vector3(pX, pY, pZ) );
	    
	    // add it to the geometry
	    particles.vertices.push(particle);
	}

	// create the particle system
	GAME.stars =new THREE.ParticleSystem(   particles,  pMaterial);

	// add it to the scene
	GAME.scene.addChild(GAME.stars);
	
	
	GAME.renderer = new THREE.WebGLRenderer();
	GAME.renderer.setSize( window.innerWidth, window.innerHeight );

	GAME.container.appendChild( GAME.renderer.domElement );

	//Event Listeners
	document.addEventListener( 'mousemove', onDocumentMouseMove, false );
	$(document).mousewheel(onDocumentMouseWheel);
	
	poll();
	log('Start long polling server');

}

function onDocumentMouseMove( event ) {
	GAME.mouse.x = event.clientX / window.innerWidth;
	GAME.mouse.y = event.clientY / window.innerHeight;
}

function onDocumentMouseWheel( event, delta ) {
	if(delta>0 && GAME.camera.distance > 220) GAME.camera.distance -= 20;
	else if(delta<0 && GAME.camera.distance < 2000) GAME.camera.distance += 20;
}


function animate() {
	requestAnimationFrame( animate );
	render();
}

function render() {
	//http://fr.wikipedia.org/wiki/Coordonn%C3%A9es_sph%C3%A9riques
	var theta = GAME.mouse.x * Math.PI * 2;
	var phi = GAME.mouse.y * Math.PI;
	GAME.camera.position.x += (GAME.camera.distance * Math.cos(theta)* Math.sin(phi) - GAME.camera.position.x ) * 0.05;
	GAME.camera.position.z += (GAME.camera.distance * Math.sin(theta)* Math.sin(phi) - GAME.camera.position.z ) * 0.05;
	GAME.camera.position.y += (GAME.camera.distance * Math.cos(phi) - GAME.camera.position.y ) * 0.05;

	//Rotation de la terre
	GAME.mesh.earth.rotation.y -= 0.005;

	GAME.renderer.render( GAME.scene, GAME.camera );

}